/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2025-02-10
 * Time: 14:50
 */

import java.util.Stack;

class Solution1_6 {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            String str = tokens[i];
            if (!str.equals("+") && !str.equals("*") && !str.equals("-")  && !str.equals("/")) {
                int num = 0 , j = 0;
                char sign = str.charAt(0);
                if(sign == '-'){
                    j++;
                }
                for (; j < str.length(); j++) {
                    char ch = str.charAt(j);
                    int curNum = ch - '0';
                    num = 10 * num + curNum;
                }
                if(sign == '-') num = -num;
                stack.push(num);
            } else {
                int num1 = stack.pop();
                int num2 = stack.pop();
                if (str.equals("+")) {
                    stack.push(num2 + num1);
                } else if (str.equals("-")) {
                    stack.push(num2 - num1);
                } else if (str.equals("*")) {
                    stack.push(num2 * num1);
                } else {
                    stack.push(num2 / num1);
                }
            }
        }
        return stack.pop();
    }
}

public class Test1_6 {
}
